//
//  MCSettingViewController.swift
//  WisdomSpace
//
//  Created by goulela on 2017/8/31.
//  Copyright © 2017年 MC. All rights reserved.
//

import UIKit

class MCSettingViewController: MCViewController {

    fileprivate var dataArray : Array<String>? = ["清理缓存","用户协议","特别声明"]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        baseSetting()
        
        initUI()
        
        sendNetworking()
    }
    
    // MARK: - Custom Delegate
    
    
    // MARK: - Action
    
    @objc func exitButtonClicked() {
        
        let storeId = MCUserinfo_getStoreId()
        
        // 退出登录 删除别名和标签
        JPUSHService.deleteAlias(nil, seq: 0)
        JPUSHService.deleteTags([String(storeId)], completion: nil, seq: 0)
        
        
        UserDefaults.LoginInfo.set(value: "0", forKey: .userId)
        UserDefaults.LoginInfo.set(value: "", forKey: .nickName)
        UserDefaults.LoginInfo.set(value: "", forKey: .picture)
        UserDefaults.LoginInfo.set(value: "0", forKey: .storeId)
        UserDefaults.LoginInfo.set(value: "", forKey: .token)
        UserDefaults.LoginInfo.set(value: "", forKey: .storeName)
        UserDefaults.LoginInfo.set(value: "", forKey: .logoPath)
        
        
        print(MCUserinfo_getUserId(),MCUserinfo_getStoreId())
        
        let vc = MCLoginPageViewController()
        let nav = MCNavigationController.init(rootViewController: vc)
        self.present(nav, animated: true, completion: nil)
    }

    // MARK: - Network
    func sendNetworking() { }
    
    // MARK: - Method
    func baseSetting() {
        self.title = "设置"
        self.view.backgroundColor = UIColor.MCGray_light
    }
    
    func initUI() {
    
        self.view.addSubview(tableView)
        tableView.snp.remakeConstraints { (make) -> Void in
            make.left.right.top.equalTo(self.view)
            make.height.equalTo((self.dataArray?.count)! * 50)
        }
        

        view.addSubview(exitButton)
        exitButton.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(20)
            make.right.equalTo(-20)
            make.bottom.equalTo(-30)
            make.height.equalTo(36)
        }
    }


    // MARK: - Setter & Getter
    private lazy var tableView: MCTableView = {
        let tableView = MCTableView.init(frame: CGRect.zero, style: UITableViewStyle.plain)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = UIColor.MCGray_light
        return tableView
    }()
    
    
    lazy var exitButton: UIButton = {
        let button = UIButton.init(type: UIButtonType.custom)
        button.backgroundColor = UIColor.MCMain
        button.layer.cornerRadius = 18
        button.layer.masksToBounds = true
        button.titleLabel?.font = MCFont(14)
        button.setTitle("退出登录", for: UIControlState.normal)
        button.setTitleColor(UIColor.white, for: UIControlState.normal)
        button.addTarget(self, action: #selector(exitButtonClicked), for: UIControlEvents.touchUpInside)
        return button
    }()
}

extension MCSettingViewController: UITableViewDelegate,UITableViewDataSource {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataArray!.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = UITableViewCell.init(style: UITableViewCellStyle.value1, reuseIdentifier: "cell")
        
        cell.selectionStyle = UITableViewCellSelectionStyle.none
        cell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
        cell.backgroundColor = UIColor.white
        
        
        
        cell.textLabel?.text = self.dataArray?[indexPath.row]
        cell.textLabel?.font = MCFont(14)
        
        cell.detailTextLabel?.font = MCFont(14)
        if indexPath.row == 0 {
            cell.detailTextLabel?.text = MCFileSizeOfCache()
        }
        
        if indexPath.row == 1 {
            cell.detailTextLabel?.text = "v " + MCSystem_APPVersion
        }
        
        if indexPath.row != ((self.dataArray?.count)! - 1) {
            let lineView = UIView.init()
            lineView.backgroundColor = UIColor.MCGray_light
            cell.addSubview(lineView)
            lineView.snp.updateConstraints({ (make) -> Void in
                make.edges.equalTo(cell).inset(UIEdgeInsetsMake(48.5, 20, 0, 20))
            })
        }
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        switch indexPath.row {
        case 0:
            MCClearCache()

            MCHUD_default(message: "清理缓存中", view: self.view, delay: 1)
            let t = DispatchTime.now() + 1
            DispatchQueue.main.asyncAfter(deadline: t, execute: {
                tableView.reloadRows(at: [indexPath], with: UITableViewRowAnimation.automatic)
                MCHUD_custom(message: "清理完成", view: self.view, delay: 1.2, image: "")
            })
        case 1:
            let h5 = MCWebViewController()
            h5.urlStr = H5_userAgreement
            h5.titleStr = "用户协议"
            MCPushToViewController(current: self, target: h5, isHiddenBottomBarWhenBack: true)

            break
        case 2:
            let statement = MCStatementViewController()
            MCPushToViewController(current: self, target: statement, isHiddenBottomBarWhenBack: true)
            break
        default:
            break
        }
    }
}
